package com.demo.supplier.utils;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.digest.BCrypt;

/**
 * 通用工具类
 * 
 * @author demo
 */
public class CommonUtil {

    /**
     * 生成唯一ID
     */
    public static String generateId() {
        return IdUtil.fastSimpleUUID();
    }

    /**
     * 生成雪花ID
     */
    public static Long generateSnowflakeId() {
        return IdUtil.getSnowflakeNextId();
    }

    /**
     * 密码加密
     */
    public static String encryptPassword(String password) {
        return BCrypt.hashpw(password, BCrypt.gensalt());
    }

    /**
     * 密码验证
     */
    public static boolean checkPassword(String password, String hashed) {
        String hashpw = BCrypt.hashpw(password);
        return BCrypt.checkpw(password, hashed);
    }

    /**
     * 获取文件扩展名
     */
    public static String getFileExtension(String filename) {
        if (filename == null || filename.isEmpty()) {
            return "";
        }
        int lastDotIndex = filename.lastIndexOf('.');
        if (lastDotIndex == -1) {
            return "";
        }
        return filename.substring(lastDotIndex + 1).toLowerCase();
    }

    /**
     * 生成文件名
     */
    public static String generateFileName(String originalFilename) {
        String extension = getFileExtension(originalFilename);
        return generateId() + (extension.isEmpty() ? "" : "." + extension);
    }
}